package me.kagami.myplugin.extensions;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import me.kagami.myplugin.form.CustomizeSettingsComponent;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

import static me.kagami.myplugin.topic.CustomizeTopics.MY_TOPIC;

public class CustomizeSettingsConfigurable implements Configurable {

    private CustomizeSettingsComponent customizeSettingsComponent;


    @Nls(capitalization = Nls.Capitalization.Title)
    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        return "定制配置";
    }

    @Override
    public @Nullable
    JComponent createComponent() {
        customizeSettingsComponent = new CustomizeSettingsComponent();
        return customizeSettingsComponent.getMypanel();
    }

    /**
     * 如果为false 则变动不会被存储，这里为了偷懒，都为true ，配置没改动也会被存储
     */
    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public void apply() throws ConfigurationException {
        CustomizeSettingsState instance = CustomizeSettingsState.getInstance();
        String text = customizeSettingsComponent.getCustomizeViewPaneName().getText();
        instance.setCustomizeViewPaneName(text);
        instance.setQuickStart1(customizeSettingsComponent.getQuickStart1().getText());
        instance.setQuickStart2(customizeSettingsComponent.getQuickStart2().getText());
        instance.setQuickStart3(customizeSettingsComponent.getQuickStart3().getText());
        instance.setQuickStart4(customizeSettingsComponent.getQuickStart4().getText());
        instance.setQuickStart5(customizeSettingsComponent.getQuickStart5().getText());
        instance.setQuickStart6(customizeSettingsComponent.getQuickStart6().getText());


        instance.setQuickProject1(customizeSettingsComponent.getQuickProject1());
        instance.setQuickProject2(customizeSettingsComponent.getQuickProject2());
        instance.setQuickProject3(customizeSettingsComponent.getQuickProject3());
        instance.setQuickProject4(customizeSettingsComponent.getQuickProject4());
        instance.setQuickProject5(customizeSettingsComponent.getQuickProject5());
        instance.setQuickProject6(customizeSettingsComponent.getQuickProject6());
        instance.setQuickProjectSimpleName1(customizeSettingsComponent.getQuickProjectSimpleName1());
        instance.setQuickProjectSimpleName2(customizeSettingsComponent.getQuickProjectSimpleName2());
        instance.setQuickProjectSimpleName3(customizeSettingsComponent.getQuickProjectSimpleName3());
        instance.setQuickProjectSimpleName4(customizeSettingsComponent.getQuickProjectSimpleName4());
        instance.setQuickProjectSimpleName5(customizeSettingsComponent.getQuickProjectSimpleName5());
        instance.setQuickProjectSimpleName6(customizeSettingsComponent.getQuickProjectSimpleName6());
        noticeListener(text);


    }

    private void noticeListener(String text) {
        Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
        for (Project openProject : openProjects) {
            openProject.getMessageBus().syncPublisher(MY_TOPIC).hintShown(openProject, text);

            ToolWindow toolWindow = ToolWindowManager.getInstance(openProject).getToolWindow("定制工具板");
            if (toolWindow != null) {
                toolWindow.getContentManager().removeAllContents(true);
                CustomizeToolWindowFactory.InitContent(toolWindow);
            }
        }
    }

    @Override
    public void disposeUIResources() {
        customizeSettingsComponent = null;
    }

    @Override
    public void reset() {
        CustomizeSettingsState instance = CustomizeSettingsState.getInstance();
        customizeSettingsComponent.getCustomizeViewPaneName().setText(instance.getCustomizeViewPaneName());
        customizeSettingsComponent.getQuickStart1().setText(instance.getQuickStart1());
        customizeSettingsComponent.getQuickStart2().setText(instance.getQuickStart2());
        customizeSettingsComponent.getQuickStart3().setText(instance.getQuickStart3());
        customizeSettingsComponent.getQuickStart4().setText(instance.getQuickStart4());
        customizeSettingsComponent.getQuickStart5().setText(instance.getQuickStart5());
        customizeSettingsComponent.getQuickStart6().setText(instance.getQuickStart6());

        customizeSettingsComponent.setQuickProject1(instance.getQuickProject1());
        customizeSettingsComponent.setQuickProject2(instance.getQuickProject2());
        customizeSettingsComponent.setQuickProject3(instance.getQuickProject3());
        customizeSettingsComponent.setQuickProject4(instance.getQuickProject4());
        customizeSettingsComponent.setQuickProject5(instance.getQuickProject5());
        customizeSettingsComponent.setQuickProject6(instance.getQuickProject6());

        customizeSettingsComponent.setQuickProjectSimpleName1(instance.getQuickProjectSimpleName1());
        customizeSettingsComponent.setQuickProjectSimpleName2(instance.getQuickProjectSimpleName2());
        customizeSettingsComponent.setQuickProjectSimpleName3(instance.getQuickProjectSimpleName3());
        customizeSettingsComponent.setQuickProjectSimpleName4(instance.getQuickProjectSimpleName4());
        customizeSettingsComponent.setQuickProjectSimpleName5(instance.getQuickProjectSimpleName5());
        customizeSettingsComponent.setQuickProjectSimpleName6(instance.getQuickProjectSimpleName6());

    }


}
